<template>
  <div class="container">
    <img src="@/assets/logo.png" alt="">
    <h1>用户注册</h1>
    <el-form :model="form" ref="form" label-width="80px">
      <el-form-item label="账号" prop="userId">
        <el-input v-model="form.userId" placeholder="请输入账号"></el-input>
      </el-form-item>
      <el-form-item label="姓名" prop="userName">
        <el-input v-model="form.userName" placeholder="请输入姓名"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="form.password" placeholder="请输入密码"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="confirmPassword">
        <el-input type="password" v-model="form.confirmPassword" placeholder="请确认密码"></el-input>
      </el-form-item>
      <el-button type="primary" @click="submitForm">注册</el-button><br />
      <router-link to="/login" class="return-link">返回登录</router-link>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "register",
  data() {
    return {
      form: {
        userId: "",
        userName: "",
        password: "",
        confirmPassword: "",
      },
    };
  },

  methods: {
    submitForm() {
      if (this.form.userId === '') {
        this.$message.error("请输入账号");
        return;
      } else if (this.form.userName === '') {
        this.$message.error("请输入姓名");
        return;
      } else if (this.form.password === '') {
        this.$message.error("请输入密码");
        return;
      } else if (this.form.password !== this.form.confirmPassword) {
        this.$message.error("两次输入的密码不一致");
        return;
      }
      this.$request.post('user/register', {
        userId: this.form.userId,
        userName: this.form.userName,
        password: this.form.password,

      })
        .then((res) => {
          let result = res;
          if (result.code == "200") {
            this.$message({
              message: result.message,
              type: "success",
            });
            window.sessionStorage.setItem("token", result.token);
            window.sessionStorage.setItem("user", JSON.stringify(result.user));
            this.form.userId = "";
            this.form.userName = "";
            this.form.password = "";
            this.form.confirmPassword = "";
            this.$router.push('/login')
          } else if (result.code == "500") {
            this.$message({
              message: result.message,
              type: "error",
            });
          }
        })
        .catch(() => {
          this.$message.error("注册失败");
        });
    },
  },

};
</script>

<style scoped>
body {
  font-family: Arial, sans-serif;
  background-color: #f5f5f5;
}

.container {
  max-width: 420px;
  /* height: 460px; */
  margin: 0 auto;
  padding: 20px;
  background-color: #fff;
  box-shadow: 0 6px 20px 0 rgba(37, 43, 50, 0.07);
  border: 1px solid #f9f9f9;
  border-radius: 5px;
  margin-top: 100px;
  flex-direction: column;
  justify-content: space-evenly;

}

.container img {
  width: 120px;
  margin: 0 auto;
  margin-bottom: 10px;
  display: block;
}

h1 {
  text-align: center;
  margin-bottom: 10px;
}

.el-input:focus {
  border-color: #ff5c00;
  outline: none;
}

.el-form-item__content {
  margin-left: 0;
}

.el-button {
  width: 100%;
  margin-top: 20px;
  text-align: center;
}

.return-link {
  display: block;
  margin-top: 10px;
  color: #333;
}

.return-link:hover {
  color: #ff5c00;
}
</style>
